import React from 'react';
import { Button, Row, Col } from 'antd';
import UserList from './list';
import CommonListsIndex from '../../../../components/CommonLists/index';
import FilterTool from '../../../../components/FilterTool';
import Add from './add';
import AuditStatus from '../../../../components/AuditStatus';
import { Tooltip } from 'antd';
import Modal from '../../../../components/Modal/index';
import RelationIndicator from '../relation';

class TacticsEventIndex extends CommonListsIndex {
  constructor(props) {
    super(props);
    this.api = 'indexDataSource';
    this.addComponent = Add;
    this.editComponent = Add;
    this.listsComponent = UserList;
    this.model_width = 400;
  }
  componentDidMount() {
    this.getAppCodeList();
    this.getProductCodeList();
  }

  // 关联指标
  showRelationIndicator = record => {
    Modal.open({
      title: <>关联指标<span style={{ fontSize: 10, color: 'grey', marginLeft: 4 }}>仅展示当前部门拥有权限数据</span></>,
      width: '1000px',
      content: (
        <RelationIndicator record={record} param={{ app_id: record.app_id, event_id: record.event_id, data_source_id: record.data_source_id }} />
      ),
      isOkBtn: true,
      maskClosable: false,
      isCancelBtn: true,
      ok: () => Modal.close()
    });
  }

  getColumns = () => {
    return [
      {
        title: '数据源名称',
        key: 'source_name',
        dataIndex: 'source_name',
        ellipsis: true,
        width: 100,
        render: (text, record) => {
          return (
            <Tooltip title={text} placement='topLeft'>
              <div className="overflowtext">
                <Button type='link' onClick={() => this.showDetail(record)}>
                  {text}
                </Button>
              </div>
            </Tooltip>
          );
        }
      },
      {
        title: (
          <>
            {this.getFilterTitle('所属渠道', this.state.applications, 'app_id', this.refreshTableFilterWithAppV2.bind(this, 'app_id'), [
              'app_id',
              'app_name'
            ])}
          </>
        ),
        dataIndex: 'app_name',
        key: 'app_id',
        ellipsis: true,
        width: 100,
        render: (text, record) => {
          return (
            <Tooltip title={text} placement='topLeft'>
              {text}
            </Tooltip>
          );
        }
      },
      {
        title: (
          <>
            {this.getFilterTitle('关联产品', this.state.products, 'product_id', this.refreshTableFilterWithAppV2.bind(this, 'product_id'), [
              'product_id',
              'product_name'
            ])}
          </>
        ),
        dataIndex: 'product_name',
        key: 'product_id',
        ellipsis: true,
        width: 100,
        render: (text, record) => {
          return (
            <Tooltip title={text} placement='topLeft'>
              {text}
            </Tooltip>
          );
        }
      },
      {
        title: (
          <>
            {this.getFilterTitle('关联事件', this.state.events, 'event_id', this.refreshTableFilterWithAppV2.bind(this, 'event_id'), [
              'event_id',
              'event_name'
            ])}
          </>
        ),
        dataIndex: 'event_name',
        key: 'event_id',
        ellipsis: true,
        width: 100,
        render: (text, record) => {
          return (
            <Tooltip title={text} placement='topLeft'>
              {text}
            </Tooltip>
          );
        }
      },
      {
        title: '指标',
        dataIndex: 'target',
        key: 'target',
        ellipsis: true,
        width: 100,
        render: (text, record) => {
          return (
            <Button
              type='link'
              onClick={this.showRelationIndicator.bind(this, record)}
            >
              {text}
            </Button>
          );
        }
      },
      {
        title: '创建人/修改人',
        dataIndex: 'create_user_name',
        key: 'create_user_name',
        ellipsis: true,
        render: (text, record) => {
          var content = !!record.update_user_name
            ? `${record.create_user_name}/${record.update_user_name}`
            : `${record.create_user_name}/${record.create_user_name}`;
          return (
            <Tooltip title={content} placement='topLeft'>
              {content}
            </Tooltip>
          );
        }
      },

      {
        title: '修改时间',
        dataIndex: 'update_time',
        key: 'update_time',
        ellipsis: true,
        render: (text, record) => {
          var content = !!record.update_time ? record.update_time : record.create_time;
          return (
            <Tooltip title={content} placement='topLeft'>
              <div className='overflowtext' style={{ width: '100%' }}>
                {content}
              </div>
            </Tooltip>
          );
        }
      },
      {
        title: '审核情况',
        key: 'status',
        dataIndex: 'audit_label',
        ellipsis: true,
        render: (text, record) => {
          return <AuditStatus text={text} record={record} />;
        }
      }
    ];
  };

  render() {
    const ListsComponent = this.listsComponent;
    const data = [
      {
        type: 'Search',
        key: 'keyword',
        name: 'keyword',
        defaultValue: '',
        placeholder: '请输入数据源名称|创建人|修改人进行搜索'
      }
    ];
    return (
      <div>
        <Row type='flex' justify='space-between' align='middle'>
          <Col span={6} style={{ textAlign: 'middle' }}>
            <Button type='primary' onClick={this.add.bind(this, { title: '添加数据源' })}>
              添加数据源
            </Button>
          </Col>
          <Col span={18}>
            <FilterTool
              ref='filterTool'
              data={data}
              colNum={1}
              query={this.freshQueryList}
              refreshFilter={this.refreshFilter}
              isOnChange={true}
              refresh={this.resetList}
              justify='end'
            />
          </Col>
        </Row>
        <div className='inner-content' style={{ marginTop: '10px' }}>
          <ListsComponent
            {...this.state}
            edit={this.edit}
            showDetail={this.showDetail}
            queryList={this.freshQueryList}
            refreshTableFilter={this.refreshTableFilter}
            onChangePagination={this.onChangePagination}
            queryTableParams={this.queryTableParams}
            rowKey='strategy_id'
            columns={this.getColumns()}
          />
        </div>
      </div>
    );
  }
}

export default TacticsEventIndex;
